//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FFTEngine.h"
using namespace Cephei::QL::Experimental::Variancegamma;
#include <gen/QL/StochasticProcess1D.h>
#include <gen/QL/Instrument.h>
using namespace Cephei::QL;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Variancegamma::CFFTEngine::CFFTEngine (boost::shared_ptr<QuantLib::FFTEngine>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFFTEngine = NULL;
#endif
	_ppFFTEngine = &childNative;
    
}
Cephei::QL::Experimental::Variancegamma::CFFTEngine::CFFTEngine (QuantLib::FFTEngine& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFFTEngine = NULL;
#endif
	_ppFFTEngine = new boost::shared_ptr<QuantLib::FFTEngine> (&childNative);
    
    _FFTEngineOwner = owner;
    
}

Cephei::QL::Experimental::Variancegamma::CFFTEngine::CFFTEngine (CFFTEngine^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFFTEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFFTEngine = new boost::shared_ptr<QuantLib::FFTEngine> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Variancegamma::CFFTEngine::CFFTEngine (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFFTEngine = NULL;
#endif
	if (!t->IsSubclassOf(CFFTEngine::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Variancegamma::CFFTEngine::CFFTEngine (QuantLib::Handle<QuantLib::FFTEngine>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFFTEngine = &childNative;
	_ppFFTEngine = &static_cast<boost::shared_ptr<QuantLib::FFTEngine>>(childNative.currentLink());
    
    _FFTEngineOwner = owner;
}
Cephei::QL::Experimental::Variancegamma::CFFTEngine::CFFTEngine (QuantLib::Handle<QuantLib::FFTEngine> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFFTEngine = &childNative;
	_ppFFTEngine = &static_cast<boost::shared_ptr<QuantLib::FFTEngine>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Variancegamma::CFFTEngine::CFFTEngine (QuantLib::FFTEngine childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFFTEngine = NULL;
#endif
	_ppFFTEngine = new boost::shared_ptr<QuantLib::FFTEngine> (new QuantLib::FFTEngine (childNative));
    
}
#endif

Cephei::QL::Experimental::Variancegamma::CFFTEngine::~CFFTEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFFTEngine != NULL)
    {
	    delete _ppFFTEngine;
        _ppFFTEngine = NULL;
    }
}
Cephei::QL::Experimental::Variancegamma::CFFTEngine::!CFFTEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFFTEngine != NULL)
    {
	    delete _ppFFTEngine;
    }
}
QuantLib::FFTEngine& Cephei::QL::Experimental::Variancegamma::CFFTEngine::GetReference ()
{
    if (_ppFFTEngine == NULL) throw REFNEW NativeNullException ();
	return **_ppFFTEngine;
}
boost::shared_ptr<QuantLib::FFTEngine>& Cephei::QL::Experimental::Variancegamma::CFFTEngine::GetShared ()
{
    if (_ppFFTEngine == NULL) throw REFNEW NativeNullException ();
	return *_ppFFTEngine;
}
QuantLib::FFTEngine* Cephei::QL::Experimental::Variancegamma::CFFTEngine::GetPointer ()
{
    if (_ppFFTEngine == NULL) throw REFNEW NativeNullException ();
	return &**_ppFFTEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FFTEngine>& Cephei::QL::Experimental::Variancegamma::CFFTEngine::GetHandle ()
{
	if (_phFFTEngine == NULL)
	{
		_phFFTEngine = new Handle<QuantLib::FFTEngine> (*_ppFFTEngine);
	}
	return *_phFFTEngine;
}
#endif
bool Cephei::QL::Experimental::Variancegamma::CFFTEngine::HasNative () 
{
	return (_ppFFTEngine != NULL);
}

Cephei::QL::Experimental::Variancegamma::IFFTEngine^ Cephei::QL::Experimental::Variancegamma::CFFTEngine::Calculate::get ()
{
    try
    {
    	(*_ppFFTEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Variancegamma::IFFTEngine^ Cephei::QL::Experimental::Variancegamma::CFFTEngine::Precalculate (Cephei::Core::IVector<Cephei::QL::IInstrument^>^ optionList)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        optionList->Lock();
        INativeVector<Cephei::QL::IInstrument^>^ _NCIoptionList = optionList->getFeature (NativeFeature::shared_ptr);
        CInstrumentVector^ _NCoptionList = safe_cast<CInstrumentVector^>(_NCIoptionList);
        std::vector<boost::shared_ptr<QuantLib::Instrument> >& _optionList = static_cast<std::vector<boost::shared_ptr<QuantLib::Instrument> >&> (_NCoptionList->GetShared ());
    	(*_ppFFTEngine)->precalculate ( _optionList );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (optionList != nullptr) optionList->Unlock(); //NO2  
    }
}
Cephei::QL::Experimental::Variancegamma::IFFTEngine^ Cephei::QL::Experimental::Variancegamma::CFFTEngine::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppFFTEngine)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

